<%-- 
    Document   : suggestions
    Created on : Dec 7, 2011, 12:12:04 AM
    Author     : Michael
--%>

<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Suggested Items</title>
<script type="text/javascript">
function row_adclick(txt)
{
    document.forms[txt].submit();
}

function row_mousehover(obj)
{
	obj.style.backgroundColor="#6699FF";
}

function row_mouseout(obj)
{
	obj.style.backgroundColor="";
}

</script>
</head>

<body>
<table width="800" border="1">
<tr>
<td><img src="Logo.jpg" width="800" height="150" />

</td>

</tr>
    <tr><td><input type="button" value="Home" onclick="window.location='gohome.jsp'"/>&nbsp;
          <input type="button" value="Log Out" onclick="window.location='logout.jsp'"/></td></tr>
  <tr>
    <td align="center">
        <h2>Suggested Items</h2></td>
  </tr>
  <tr>
      <td>
      <table width="800" border="1">
        <tr >
        <td >Company</td>
        <td >Item Name</td>
        <td >Item Type</td>
        <td >Content</td>
        <td >Unit Price</td>     
        <td >Available Units</td>
        <td >Date</td>
        </tr>
        <% 
    String query = "SELECT * " +
        "FROM advertisement " + 
        "WHERE ItemType IN (" +
        "SELECT ItemType " + 
        "FROM customer c, sales s, advertisement a " + 
        "WHERE c.CustomerID=" + session.getAttribute("UserID") + " AND c.AccountNo=s.AccountNo " +
        "AND s.AdID=a.AdvertisementID)";
    
        Connection conn = null;
        try
        {
            conn = new DBClasses.GetConnection().getConnection();
            ResultSet rs = conn.createStatement().executeQuery(query);

                int i=0;
                while(rs.next())
                {
                    if(rs.getBoolean("Active")||(!session.getAttribute("Access").equals("Customer")))
                    {

        %>
        <tr >
        <form name="<%="ad" + i %>" action="AdClicked.jsp" method="post">
        <input name="AD" type="hidden" value="<%=rs.getInt("AdvertisementID")%>" />
        <td ><%=rs.getString("Company")%></td>
        <td ><%=rs.getString("ItemName")%></td>
        <td ><%=rs.getString("ItemType")%></td>
        <td ><%=rs.getString("Content")%></td>
        <td ><%=rs.getInt("UnitPrice")%></td>
        <td ><%=rs.getInt("AvailableUnits")%></td>
        <td ><%=new java.text.SimpleDateFormat("M-d-yyyy").format(rs.getDate("Date"))%></td>
        <% String buttonValue = "";
            if(session.getAttribute("Access").equals("Customer")) 
                buttonValue = "Purchase";
            else
                buttonValue = "Edit";
        %>
        <td ><button onclick="row_adclick('<%="ad" + i %>')"><%=buttonValue%> </button>
        </td>
        </form>
                
        </tr>

        <%
                        }
                        i++;
                   }
           %>
      </table>
      </td>
  </tr>
</table>
</body>
</html>

<% }
    
    catch(Exception e) { e.printStackTrace(); response.sendRedirect("gohome.jsp"); }
    finally
    {
        try {
        conn.close();
        }
        catch(Exception e) { e.printStackTrace(); response.sendRedirect("gohome.jsp"); }
    }
    
    %>
